package com.zone.config;

import com.zone.types.enums.ResponseCode;
import com.zone.types.exception.BizException;
import com.zone.types.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: zongzi
 * @description: 全局异常处理器
 * @date: 2024/6/18
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 业务异常处理器
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BizException.class)
    public Response<Object> bizExceptionHandler(BizException e) {
        return Response.builder()
                .code(e.getResponseCode().getCode())
                .info(e.getErrorMsg())
                .build();
    }

    @ExceptionHandler(Exception.class)
    public Response<Object> exceptionHandler(Exception e) {
        log.error("服务执行异常：", e);
        return Response.builder()
                .code(ResponseCode.ERROR.getCode())
                .info(e.getMessage())
                .build();
    }
}
